package com.example.day1201_wlan.slice;

import com.example.day1201_wlan.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.event.commonevent.*;
import ohos.hiviewdfx.HiLog;
import ohos.rpc.RemoteException;
import ohos.wifi.WifiEvents;

public class Message extends AbilitySlice {

    private WifiEventSubscriber wifiEventSubscriber;
    private Text mText;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_message);

        initCompent();
    }

    private void initCompent() {
        Button register = findComponentById(ResourceTable.Id_register_btn);
        Button unregister = findComponentById(ResourceTable.Id_unregister_btn);

        register.setClickedListener(component -> register());
        unregister.setClickedListener(component -> unRegister());

        mText = findComponentById(ResourceTable.Id_log_text);

    }
// 取消注册
    private void unRegister() {

        try {
            CommonEventManager.unsubscribeCommonEvent(wifiEventSubscriber);
            Toas("取消订阅");
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }

    // 注册
    private void register() {
        MatchingSkills match = new MatchingSkills();
    // 侦听Wi-Fi状态 如 开启 或 关闭
        match.addEvent(WifiEvents.EVENT_ACTIVE_STATE);
// 侦听Wi-Fi连接状态 如 断开 或 连接
        match.addEvent(WifiEvents.EVENT_CONN_STATE);
//侦听Wi-Fi扫描结果  如 扫描成功 或失败
        match.addEvent(WifiEvents.EVENT_SCAN_STATE);

        CommonEventSubscribeInfo info = new CommonEventSubscribeInfo(match);
        wifiEventSubscriber = new WifiEventSubscriber(info);

        try {
            CommonEventManager.subscribeCommonEvent(wifiEventSubscriber);
            Toas("订阅wifi事件监听");
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }


    class WifiEventSubscriber extends CommonEventSubscriber {
        
       WifiEventSubscriber(CommonEventSubscribeInfo subscribeInfo) {
           super(subscribeInfo);
       }

       @Override
        public void onReceiveEvent(CommonEventData commonEventData) {
           //只处理wifi活动状态状态
           if (!WifiEvents.EVENT_ACTIVE_STATE.equals(commonEventData.getIntent().getAction())) {
               return;
           }
           IntentParams params = commonEventData.getIntent().getParams();
           if (params == null || params.getParam(WifiEvents.PARAM_ACTIVE_STATE) == null) {
               return;
           }

           int wifiState = (int) params.getParam(WifiEvents.PARAM_ACTIVE_STATE);
           switch (wifiState) {
               case WifiEvents.STATE_ACTIVE: {
                   mText.setText("启用Wi-Fi功能" + System.lineSeparator());
                   break;
               }
               case WifiEvents.STATE_INACTIVE: {
                   mText.setText("关闭Wi-Fi功能" + System.lineSeparator());
                   break;
               }
               default:
                   mText.setText("Wif功能未知" + System.lineSeparator());
                   break;
           }

        }
    }
    private void Toas(String message) {
        new ToastDialog(getContext()).setText(message).show();
    }
}
